package spring.com.rocketmq.service.impl;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.stereotype.Service;
import spring.com.rocketmq.rocketmq.ParamConfigService;
import spring.com.rocketmq.rocketmq.RocketMqProduct;
import spring.com.rocketmq.service.RocketMqProductService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liux
 * @date 2021/6/1 16:44
 */
@Slf4j
@Service
@AllArgsConstructor
public class RocketMqProductsImpl implements RocketMqProductService {
    //引入生产者
    private final RocketMqProduct product;
    private final ParamConfigService paramConfigService;

    @Override
    public String openAccountMsg(List<String> msgList) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {

        //根据传过来的集合循环消费:就是顺序发放消费信息:单个的消费
        for(String s:msgList){
            //创建消息体
            Message message = new Message(paramConfigService.rocketTopic, paramConfigService.rocketTag, s.getBytes());
            //发放消息
            SendResult send = product.getRocketMqProduct().send(message);
            //输出生产者信息
            log.info("生产者的消息为："+send);
        }
        return "成功";
    }
}
